package com.bw.zg3park2212.fangke

import android.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.PopupWindow
import android.widget.RadioButton
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.FangAddParam
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.adpter.FangAdpter
import com.bw.zg3park2212.databinding.ActivityFangBinding

import com.bw.zg3park2212.fangke.FangViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path="/app/FangActivity")
class FangActivity : BaseActivity<ActivityFangBinding,FangViewModel>() {
    lateinit var fangAdpter: FangAdpter
    var name=""
    var stause=""
    override fun intitView() {
        fangAdpter= FangAdpter()
        binding.rv.layoutManager= LinearLayoutManager(this)
        binding.rv.adapter=fangAdpter
        binding.rgAll.setOnClickListener {
            viewmodel.getFang()
        }
        binding.rgDaishenhe.setOnClickListener {
            viewmodel.getFangSuatas("待审核")
        }
        binding.rgYijujue.setOnClickListener {
            viewmodel.getFangSuatas("驳回")
        }
        binding.rgYitongguo.setOnClickListener {
            viewmodel.getFangSuatas("已同意")
        }
        binding.rgYiwancheng.setOnClickListener {
            viewmodel.getFangSuatas("已完成")
        }
        binding.sv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                name=query!!
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                name=""
                viewmodel.getFang()
                return false
            }
        })
        binding.btn1.setOnClickListener {
            viewmodel.getname(name)
        }
        binding.btn2.setOnClickListener {
            viewmodel.getMudi(name)
        }
        ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
            ): Int {
                var left=ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder,
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition
                viewmodel.getfangdel(fangAdpter.data[position].id)
                //ToastUtils.showLong(fangAdpter.data[position].id)
                fangAdpter.notifyItemRemoved(position)


            }
        }).attachToRecyclerView(binding.rv)
        fangAdpter.setOnItemLongClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/FangXiangActivity")
                .withSerializable("fxid",fangAdpter.data[position])
                .navigation()
            return@setOnItemLongClickListener true
        }
        fangAdpter.setOnItemClickListener { adapter, view, position ->
            val popupWindow= PopupWindow(this)
            val inflate = LayoutInflater.from(this).inflate(R.layout.item_popfangadd, null)
            val btnYes = inflate.findViewById<Button>(R.id.btn_yes)
            val btnNo = inflate.findViewById<Button>(R.id.btn_no)
            val  visitorName= inflate.findViewById<EditText>(R.id.fangxg_name)
            val  purpose= inflate.findViewById<EditText>(R.id.fangxg_mudi)
            val  status_daishenhe= inflate.findViewById<RadioButton>(R.id.fxg_rg_daishenhe)
            val  status_yijujue= inflate.findViewById<RadioButton>(R.id.fxg_rg_yijujue)
            val  status_yiwancheng= inflate.findViewById<RadioButton>(R.id.fxg_rg_yiwancheng)
            val  status_yitongguo= inflate.findViewById<RadioButton>(R.id.fxg_rg_yitongguo)
            status_daishenhe.setOnClickListener {
                stause="待审核"
            }
            status_yijujue.setOnClickListener {
                stause="驳回"
            }
            status_yiwancheng.setOnClickListener {
                stause="已完成"
            }
            status_yitongguo.setOnClickListener {
                stause="已通过"
            }
            btnYes.setOnClickListener {
                val fangAddParam=FangAddParam()
                fangAddParam.visitorName=visitorName.text.toString()
                fangAddParam.purpose=purpose.text.toString()
                fangAddParam.visitStatus=stause
                fangAddParam.phoneNumber=13988445533
                fangAddParam.appointmentTime=SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
                viewmodel.getfangUp(fangAdpter.data[position].id, fangAddParam)
            }
            btnNo.setOnClickListener {
                popupWindow.dismiss()
            }
            popupWindow.isFocusable=true
            popupWindow.isOutsideTouchable=true
            popupWindow.width= ActionBar.LayoutParams.MATCH_PARENT
            popupWindow.height=500
            popupWindow.contentView=inflate
            popupWindow.showAtLocation(binding.rv, Gravity.BOTTOM,0,0)
        }
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/app/FangAddActivity").navigation()
        }
    }

    override fun intitData() {
        viewmodel.fangSuccse.observe(this){
            fangAdpter.data.clear()
            fangAdpter.addData(it)
            fangAdpter.notifyDataSetChanged()
        }
        viewmodel.fangSuatasSuccse.observe(this){
            fangAdpter.data.clear()
            fangAdpter.addData(it)
            fangAdpter.notifyDataSetChanged()
        }
        viewmodel.fangnameSuccse.observe(this){
            fangAdpter.data.clear()
            fangAdpter.addData(it)
            fangAdpter.notifyDataSetChanged()
        }
        viewmodel.fangmudiSuccse.observe(this){
            fangAdpter.data.clear()
            fangAdpter.addData(it)
            fangAdpter.notifyDataSetChanged()
        }
        viewmodel.fangdelSuccse.observe(this){
            ToastUtils.showLong("删除成功")
        }
        viewmodel.fangUpSuccse.observe(this){
            ToastUtils.showLong("修改成功")
            fangAdpter.addData(it)
            fangAdpter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        viewmodel.getFang()
    }
}